import scrapy
from ScrapyObject.spiders.utils.url_utils import *
'''
scrapy crawl ydaf -o ydaf.json
https://zclsqo.nmyy5.fit/cn/home/web/
'''
class YdafSpider(scrapy.Spider):
    name = "ydaf"
    # 前缀
    prefix = 'https://zclsqo.'
    # 中缀
    website = 'nmyy5'
    # 后缀
    suffix = '.fit/'
    allowed_domains = [website + ".fit"]
    start_urls = [prefix + website + suffix + "cn/home/web/"]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_three(content)
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=video_url[0])
        urls = response.xpath("//div[@class='video-play video-play-width-1']//a/@ href").extract()
        name = response.xpath("//div[@class='video-play video-play-width-1']//a/@ title").extract()
        p_url = response.xpath("//div[@class='video-play video-play-width-1']//a//img/@ src").extract()
        tag = response.xpath("//h2[@class='left-title']/text()").extract()
        if len(urls) == len(name) and len(urls) == len(p_url) and len(tag):
            for i in range(len(urls)):
                self.i = self.i + 1
                yield get_video_item(id=self.i, tags=tag[0].replace("当前位置:", "").replace("搜索结果:", ""), url=split_joint(self.prefix + self.website + self.suffix, urls[i]), name=name[i], pUrl=p_url[i], vUrl='')
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/') and url.endswith('.html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
